// SetupXML.cpp: implementation of the CSetupXML class.   
//   
//////////////////////////////////////////////////////////////////////   

#include "stdafx.h"   
#include "SetupXML.h"
#include "Cust_App.h"
#include "Cust_Target.h"
#include "atlconv.h"

#ifdef _DEBUG   
#undef THIS_FILE   
static char THIS_FILE[]=__FILE__;   
#define new DEBUG_NEW   
#endif   

//////////////////////////////////////////////////////////////////////   
// Construction/Destruction   
//////////////////////////////////////////////////////////////////////   

CSetupXML::CSetupXML()   
{   
	m_pDoc = NULL;   
}   

CSetupXML::~CSetupXML()   
{
}   

BOOL CSetupXML::Open(LPCTSTR lpszPathName)   
{
	WIN32_FIND_DATA 	FindFileData;
	HANDLE				hFind;
	HRESULT				hr;

	m_sFileName = lpszPathName;
	hFind = FindFirstFile(lpszPathName, &FindFileData);

	hr = m_pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
	if(FAILED(hr))
	{
		LOGERR("Failed to create the XML class instance");
		return	FALSE;
	}
	if (hFind == INVALID_HANDLE_VALUE)    
	{
		return	FALSE;
	}
	else
	{
		FindClose(hFind);
		
		m_pDoc->load((_variant_t)m_sFileName);
	}

	return TRUE;
}

void CSetupXML::Save()
{
	if (m_pDoc)
	{
		COleVariant vName(m_sFileName);
		m_pDoc->save(vName);    
	}
}

BOOL CSetupXML::AddID(LPCTSTR sID, LPCTSTR sPW)
{
	MSXML2::IXMLDOMProcessingInstructionPtr		pPI;
	MSXML2::IXMLDOMElementPtr				pList;
	MSXML2::IXMLDOMElementPtr				pUser;
	BSTR bsList = ::SysAllocString(L"List");
	BSTR bsUser = ::SysAllocString(L"User");
		
	if(!Open())
	{
		// create processing instruction   
		BSTR bsPITarget = ::SysAllocString(L"xml");    
		BSTR bsPIData = ::SysAllocString(L"version=\"1.0\" encoding=\"euc-kr\"");   

		pPI = m_pDoc->createProcessingInstruction(bsPITarget, bsPIData);
		::SysFreeString(bsPITarget);   
		::SysFreeString(bsPIData);   
		if(pPI == NULL)
		{
			::SysFreeString(bsList);
			::SysFreeString(bsUser);
			return	FALSE;
		}

		// create root
		pList = m_pDoc->createElement(bsList);
		pUser = m_pDoc->createElement(bsUser);
		::SysFreeString(bsList);
		::SysFreeString(bsUser);
		if((pList == NULL) || (pUser == NULL))
		{
			return	FALSE;
		}

		BSTR	bsID = CharToBSTR(sID);
		BSTR	bsPW = CharToBSTR(sPW);
		pUser->setAttribute(L"ID", bsID);
		pUser->setAttribute(L"PW", bsPW);
		m_pDoc->appendChild(pPI);
		m_pDoc->appendChild(pList);
		pList->appendChild(pUser);
		
		pPI.Release();
		pList.Release();
		pUser.Release();
		::SysFreeString(bsID);
		::SysFreeString(bsPW);
	}
	else
	{
		pUser = m_pDoc->createElement(bsUser);
		::SysFreeString(bsList);
		::SysFreeString(bsUser);
		if(pUser == NULL)
		{
			return	FALSE;
		}

		BSTR	bsID = CharToBSTR(sID);
		BSTR	bsPW = CharToBSTR(sPW);
		pUser->setAttribute(L"ID", bsID);
		pUser->setAttribute(L"PW", bsPW);
		m_pDoc->GetlastChild()->appendChild(pUser);
		
		pUser.Release();
		::SysFreeString(bsID);
		::SysFreeString(bsPW);
	}
	return	TRUE;
}

BOOL CSetupXML::DelID(LPCTSTR sID, LPCTSTR sPW)
{
	MSXML2::IXMLDOMElementPtr				pEle;
	BSTR	bsID;
	VARIANT	bsVal;
	char		*com, *bs;
		
	if(!Open())
	{
		LOGERR("LogIn information is not exist..");
		return	FALSE;
	}
	else
	{
		com = UIstring(NULL);

		sprintf(com, "%s%s%s", "//*[@ID=\'", sID, "\']");
		bsID = CharToBSTR(com);
		pEle = m_pDoc->selectSingleNode(bsID);
		if(pEle == NULL)
		{
			LOGERR("[%s] ID is not exist..", sID);
			UIstring(com);
			::SysFreeString(bsID);
			return	FALSE;
		}
		UIstring(com);
		bsVal = pEle->getAttribute(L"PW");
		bs = BSTRToChar(bsVal.bstrVal);
		if(strcmp(bs, sPW) != 0)
		{
			LOGERR("Password is incorrect.");
			::SysFreeString(bsID);
			return	FALSE;
		}
		m_pDoc->GetlastChild()->removeChild(pEle);
		pEle.Release();
		UIstring(bs);
		::SysFreeString(bsID);
	}
	return	TRUE;
}

BOOL CSetupXML::FixPW(LPCTSTR sID, LPCTSTR sPW)
{
	MSXML2::IXMLDOMElementPtr				pEle;
	BSTR	bsID, bsPW;
	char		*com;
		
	if(!Open())
	{
		LOGERR("LogIn information is not exist..");
		return	FALSE;
	}
	else
	{
		com = UIstring(NULL);

		sprintf(com, "%s%s%s", "//*[@ID=\'", sID, "\']");
		bsID = CharToBSTR(com);
		pEle = m_pDoc->selectSingleNode(bsID);
		if(pEle == NULL)
		{
			LOGERR("[%s] ID is not exist..", sID);
			UIstring(com);
			::SysFreeString(bsID);
			return	FALSE;
		}
		UIstring(com);
		bsPW = CharToBSTR(sPW);
		pEle->setAttribute(L"PW", bsPW);
		pEle.Release();
		::SysFreeString(bsID);
		::SysFreeString(bsPW);
	}
	return	TRUE;
}

void CSetupXML::FindInfo(LPCTSTR sFindID, LPCTSTR sFindPW, BOOL *bID, BOOL *bPW)
{
	MSXML2::IXMLDOMElementPtr	pEle;
	BSTR bsID = CharToBSTR(sFindID);
	VARIANT bsVal;
	char		*bs;

	pEle = m_pDoc->selectSingleNode(bsID);
	if(pEle == NULL)
	{
		*bID = FALSE;
		::SysFreeString(bsID);
		return;
	}
	else
	{
		*bID = TRUE;
	}
	bsVal = pEle->getAttribute(L"PW");
	bs = BSTRToChar(bsVal.bstrVal);
	if(strcmp(bs, sFindPW) != 0)
	{
		*bPW = FALSE;
	}
	else
	{
		*bPW = TRUE;
	}
	pEle.Release();
	UIstring(bs);
	::SysFreeString(bsID);
}

BSTR CSetupXML::CharToBSTR(const char *conv )
{
	BSTR	Msg;
	
	USES_CONVERSION;
	Msg = ::SysAllocString( A2W(conv) );
	
	return	Msg;
}


char *CSetupXML::BSTRToChar(const BSTR conv)
{
	char		*Msg;

	Msg = UIstring(NULL);
		
	USES_CONVERSION;
	strcpy( Msg, OLE2T(conv) );

	return	Msg;
}

